﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;

namespace Portal_KonTum.Admin
{
    public partial class NewsMn : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            notes();
            if (!IsPostBack)
            {
                bin();
            }
        }
        void notes()
        {
            if (Request.QueryString["Notes"] != null && Request.QueryString["Active"] != null)
            {
                string snotes = Request.QueryString["Notes"];
                string sactive = Request.QueryString["Active"];
                string sout = MethodCommon.StrQueryNotes(snotes, sactive);
                lbnotes.Visible = true;
                lbnotes.InnerText = sout;
            }
        }
        void bin()
        {
            iq = QueryMgr.GetDbInstance();
            List<Category> cate = iq.Ca_GetAllCate();
            DL_cate.DataSource = cate;
            DL_cate.DataValueField = "CateID";
            DL_cate.DataTextField = "CategoryName";
            DL_cate.DataBind();

            List<New> ex = new List<New>();
            iq = QueryMgr.GetDbInstance();
            ex = iq.News_GetAllNew();
            GridView1.DataSource = ex;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                HiddenField hfs = (HiddenField)e.Row.FindControl("hf_cb");
                Label lb_cs = (Label)e.Row.FindControl("lb_cb");
                iq = QueryMgr.GetDbInstance();
                User st = new User();
                st = iq.UR_GetUserByID(MethodCommon.Get_Guid(hfs.Value));
                lb_cs.Text = st.FullName + "";

                HiddenField hfl = (HiddenField)e.Row.FindControl("hf_hl");
                HyperLink hl = (HyperLink)e.Row.FindControl("HyperLink1");

                //New n = new New();
                //n= iq.News_GetNewByID(MethodCommon.Get_Guid(hfl.Value));
                hl.NavigateUrl = "~/PrintNews.aspx?ID=" + hfl.Value;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            New cus = new New();
            String index = e.CommandArgument.ToString();

            if (e.CommandName == "rowEdit")
            {
                Response.Redirect("NewsNew.aspx?NewsID=" + index);
            }
            if (e.CommandName == "rowDelete")
            {
                Guid _id = new Guid(index);
                iq = QueryMgr.GetDbInstance();
                iq.News_DelOrResNew(_id);
                bin();
                Response.Redirect("NewsMn.aspx?Notes=StrDel&Active=true");
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            bin();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;

            GridView1.EditIndex = -1;
            GridView1.SelectedIndex = -1;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<New> ex = new List<New>();
            string cate = DL_cate.SelectedValue.ToString();
            iq = QueryMgr.GetDbInstance();
            Guid idc = new Guid(cate);
            ex = iq.News_GetNewByCatID(idc);
            GridView1.DataSource = ex;
            GridView1.DataBind();
        }
    }
}